<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     */
    protected function redirectTo(Request $request): ?string
    {
        if($request->user() === null){

            $response = response()->json(['code' => 2, 'msg' => '登录失效', 'data' => []]);
            //$response->header('Access-Control-Allow-Origin', 'http://localhost:9000');
            $response->send();
        }

        return $request->expectsJson() ? null : route('login');
    }

//    public function handle($request, Closure $next, ...$guards)
//    {
//        $this->authenticate($request,$guards);
//        //向requests 中注入解析出来的用户id
//        $request->offsetSet('u_id',$request->user()->id);
//
//        return $next($request);
//    }
}
